package com.duan.DataCheckGW.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.duan.DataCheckGW.entity.RiskFieldScript;
import com.duan.DataCheckGW.groovy.GroovyUtil;
import com.duan.DataCheckGW.mapper.RiskFieldScriptMapper;

@RestController
public class RiskFieldScriptController {

	@Autowired
	private RiskFieldScriptMapper scriptMapper;
	
	@RequestMapping(value="/dataCheck/{productCode}/{riskType}")
	public String dataCheck(@PathVariable("productCode")String productCode,@PathVariable("riskType")String riskType){
		Object msg = "";
		RiskFieldScript riskFieldScript = this.scriptMapper.getRiskFieldScript(productCode, riskType);
		String script = riskFieldScript.getRiskFieldScript();
		String checkError = riskFieldScript.getRiskCheckError();
		//String script = "def dataCheck(param1,param2){def jsonStr = param1;def jsonCheckStr = param2;def jsonSlurper = new groovy.json.JsonSlurper();def obj = jsonSlurper.parseText(jsonStr);def checkObj = jsonSlurper.parseText(jsonCheckStr);obj.each{if(it.value==null || it.value==''){return checkObj.getAt(it.key);}}}";
		Map<String,String> jsonMap = new HashMap<String, String>();
		jsonMap.put("name", "");
		String jsonStr = JSON.toJSONString(jsonMap);
		Object[] objects = new Object[]{jsonStr, checkError};
		msg = GroovyUtil.runGroovyScript(script, "dataCheck", objects);
		return msg.toString();
	}
}
